package com.sms.graduate.controller;

import com.sms.graduate.entity.SmsLeave;
import com.sms.graduate.entity.SmsUser;
import com.sms.graduate.service.SmsDictionaryService;
import com.sms.graduate.service.SmsLeaveService;
import com.sms.graduate.service.SmsUserService;
import com.sms.graduate.utils.DataTools;
import com.sms.graduate.utils.R;
import com.sms.graduate.utils.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/leave")
@Api(tags = "请假模块")
@CrossOrigin(origins = "*")
public class LeaveController {

    @Autowired
    SmsDictionaryService smsDictionaryService;

    @Autowired
    SmsUserService smsUserService;

    @Autowired
    SmsLeaveService smsLeaveService;

    @PostMapping("/leaveApply")
    @ApiOperation("新增请假申请")
    public R leaveApply(SmsLeave smsLeave,HttpServletRequest request){
        try {
            String userCode = TokenUtils.getUserCode(request);
            SmsUser smsUser = smsUserService.selectUserByCode(userCode);
            String userClass = smsUser.getUserClass();
            String s = smsUserService.selectTeacher(userClass);
            SmsUser user = smsUserService.selectUserByCode(s);
            smsLeave.setUsercode(smsUser.getUsercode());
            smsLeave.setUsername(smsUser.getUsername());
            smsLeave.setApprover(user.getUsername());
            smsLeave.setApprovercode(s);
            smsLeave.setResult("待审批");
            smsLeaveService.addApply(smsLeave);
            return R.ok("提交成功");
        }catch (Exception e){
            e.getMessage();
            return  R.error();
        }
    }

    @PostMapping("/selectLeaveApply")
    @ApiOperation("查询请假记录")
    public R selectLeaveApply(HttpServletRequest request){
        try {
            String userCode = TokenUtils.getUserCode(request);
            List<SmsLeave> smsLeaves = smsLeaveService.selectLeaveApply(userCode);
            SmsUser smsUser = smsUserService.selectUserByCode(userCode);
            String s = DataTools.gradeAndMajor(userCode);
            smsLeaves.stream().forEach(a->{
                a.setUserClass(s);
                a.setSex(smsUser.getSex());
            });
            HashMap<String,Object> map = new HashMap<>();
            map.put("msg","查询成功");
            map.put("data",smsLeaves);
            return R.ok(map);
        }catch (Exception e){
            e.printStackTrace();
            return R.error();
        }
    }

    @PostMapping("/passApply")
    @ApiOperation(value = "请假通过")
    public R passApply(@RequestParam("id") Integer id){
        try {
            smsLeaveService.passApply(id);
            return R.ok("审批成功");
        }catch (Exception e){
            e.getMessage();
            return R.error();
        }
    }

    @PostMapping("/rejectApply")
    @ApiOperation(value = "请假拒绝")
    public R rejectApply(@RequestParam("id") Integer id,@RequestParam("reject") String reject){
        try {
            smsLeaveService.rejectApply(id, reject);
            return  R.ok("驳回成功");
        }catch (Exception e){
            e.getMessage();
            return  R.error();
        }
    }









}
